package cn.eeepay.boss.action;

import cn.eeepay.boss.system.DataSource;
import cn.eeepay.boss.system.SystemLog;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.org.*;
import cn.eeepay.framework.service.PolicyTargetService;
import cn.eeepay.framework.util.Constants;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/policyTarget")
public class PolicyTargetAction {

    private  static final Logger log = LoggerFactory.getLogger(PolicyTargetAction.class);

    @Resource
    private PolicyTargetService policyTargetService;

    @RequestMapping("/detailList")
    @ResponseBody
    public Map<String, Object> detailList(BigInteger id){
        Map<String, Object> result = new HashMap<>();
        try {
            List<PolicyTargetDetail> detailList = policyTargetService.selectPolicyTargetDetail(id);
            result.put("list", detailList);
        } catch (Exception e) {
            log.error("获取目标金额配置详情失败", e);
            result.put("status", false);
        }
        return result;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping("/recordDetial")
    @ResponseBody
    public Map<String, Object> recordDetial(BigInteger id){
        Map<String, Object> result = new HashMap<>();
        result.put("status", true);
        PolicyTarget target = policyTargetService.selectPolicyTarget(id);
        List<PolicyMTime> mTimes = policyTargetService.selectMTimes();
        result.put("info", target);
        result.put("mTimes", mTimes);
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    @SystemLog(description = "删除目标金额配置", operCode = "policyTarget.delete")
    public Map<String, Object> delete(BigInteger id){
        Map<String, Object> result = new HashMap<>();
        try {
            int num =policyTargetService.deletePolicyTarget(id);
            if(num == 1){
                result.put("status", true);
                result.put("msg", "删除成功");
            }else{
                result.put("status", false);
                result.put("msg", "删除失败");
            }

        } catch (Exception e) {
            result.put("status", false);
            result.put("msg", "删除失败");
            log.error(e.getMessage());
        }
        return result;
    }


    /**
     * 手动新增
     * @param target
     * @return
     */
    @RequestMapping(value="/savePolicyTarget")
    @ResponseBody
    @SystemLog(description = "手动新增", operCode = "policyTarget.save")
    public Map<String, Object> savePolicyTarget(@RequestBody PolicyTarget target){
        Map<String, Object> result = new HashMap<>();
        try {
            policyTargetService.savePolicyTarget(target);
            result.put("status", true);
        } catch (Exception e) {
            result.put("status", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping("/export")
    @SystemLog(operCode = "policyTarget.export", description = "导出")
    public void export(String baseInfo, HttpServletResponse response){
        try {
            if(StringUtils.isNotEmpty(baseInfo)){
                baseInfo = URLDecoder.decode(baseInfo, "utf-8");
            }
            PolicyTarget target = JSONObject.parseObject(baseInfo, PolicyTarget.class);
            policyTargetService.export(response, target);
        } catch (Exception e){
            log.error("目标金额配置列表导出异常", e);
        }
    }


    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping(value="/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam("info") String param,
                                      @ModelAttribute("page") Page<PolicyTarget> page){
        log.info("入参：" + JSONObject.toJSONString(param));
        Map<String, Object> result = new HashMap<>();
        try {
            PolicyTarget target = JSONObject.parseObject(param, PolicyTarget.class);
            policyTargetService.list(target, page);
        } catch (Exception e) {
            log.error("查询目标金额配置列表失败",e);
        }
        result.put("status", true);
        result.put("page", page);
        return result;
    }

    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping(value="/selectQuotas")
    @ResponseBody
    public Map<String, Object> selectQuotas(@ModelAttribute("page") Page<PolicyQuotaInfo> page){
        Map<String, Object> result = new HashMap<>();
        try {
            List<PolicyQuotaInfo> list = policyTargetService.selectHisQuotas(page);
            List<PolicyQuotaInfo> quotas = policyTargetService.selectNewQuotas();
            result.put("page", page);
            result.put("quotas", quotas);
            result.put("status", true);
        } catch (Exception e) {
            log.error("查询目标金额配置列表失败",e);
            result.put("status", true);
            return result;
        }
        return result;
    }


    /**
     * 数据更新时间节点
     * @param mTimes
     * @return
     */
    @RequestMapping(value="/saveMTimes")
    @ResponseBody
    @SystemLog(description = "数据更新时间节点", operCode = "policyTarget.addMTime")
    public Map<String, Object> saveMTimes(@RequestBody List<PolicyMTime> mTimes){
        log.info("入参：" + mTimes);
        Map<String, Object> result = new HashMap<>();

        try {
            policyTargetService.saveMTimes(mTimes);
            result.put("status", true);
        } catch (Exception e) {
            result.put("status", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value="/saveQuotas")
    @ResponseBody
    @SystemLog(description = "目标总额设置", operCode = "policyTarget.addQuota")
    public Map<String, Object> saveQuotas(@RequestBody List<PolicyQuotaInfo> quotas){
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("入参：" + quotas);
            for (PolicyQuotaInfo quota : quotas) {
                if(null == quota.getQuotaAmount()){
                    result.put("status", false);
                    result.put("msg", "目标金额为空");
                    return result;
                }
                if(null == quota.getEffectiveTime()){
                    result.put("status", false);
                    result.put("msg", "生效时间为空");
                    return result;
                }
                if(quota.getEffectiveTime().before(new Date())){
                    result.put("status", false);
                    result.put("msg", "生效时间小于当前时间");
                    return result;
                }
            }
            policyTargetService.saveQuotas(quotas);
            result.put("status", true);
        } catch (Exception e) {
            result.put("status", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 目标金额预估
     * @param param
     * @return
     */
    @RequestMapping(value="/updateTargetConfig")
    @ResponseBody
    @SystemLog(description = "目标金额预估", operCode = "policyTarget.updateTargetConfig")
    public Map<String, Object> updateTargetConfig(@RequestParam("info") String param){
        log.info("入参：" + param);
        Map<String, Object> result = new HashMap<>();
        policyTargetService.updateTargetConfig(param);
        result.put("status", true);
        return result;
    }

    /**
     * 修改 目标金额预估取值范围
     * @param config
     * @return
     */
    @RequestMapping(value="/updateTargetRange")
    @ResponseBody
    @SystemLog(description = "目标金额预估取值范围", operCode = "policyTarget.vipRange")
    public Map<String, Object> updateTargetRange(@RequestBody PolicyTargetRange config){
        log.info("入参：" + config);
        Map<String, Object> result = new HashMap<>();
        try {
            policyTargetService.updateTargetRange(config);
        } catch (Exception e) {
            log.error("修改目标金额预估取值范围异常");
            result.put("status", false);
            return result;
        }
        result.put("status", true);
        return result;
    }

    /**
     * 获取所有时间节点
     * @return
     */
    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping(value="/selectMTimes")
    @ResponseBody
    public Map<String, Object> selectMTimes(){
        Map<String, Object> result = new HashMap<>();
        List<PolicyMTime> list = policyTargetService.selectMTimes();
        result.put("status", true);
        result.put("data", list);
        return result;
    }


    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping(value="/selectTargetConfig")
    @ResponseBody
    public Map<String, Object> selectTargetConfig(){
        Map<String, Object> result = new HashMap<>();
        String data = policyTargetService.selectTargetConfig();
        PolicyTargetConfig config = JSONObject.parseObject(data, PolicyTargetConfig.class);
        config.setPolicyTransRatioValue(config.getPolicyTransRatio().multiply(new BigDecimal("100")));
        result.put("status", true);
        result.put("data", config);
        return result;
    }


    @DataSource(Constants.DATA_SOURCE_SLAVE)
    @RequestMapping(value="/selectTargetRange")
    @ResponseBody
    public Map<String, Object> selectTargetRange(){
        Map<String, Object> result = new HashMap<>();
        PolicyTargetRange config = policyTargetService.selectTargetRange();
        result.put("status", true);
        result.put("data", config);
        return result;
    }


}
